//
//  ColorPickerGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/13/13.
//
//

#include "ColorPickerGameScene.h"


CCScene* ColorPickerGameScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    ColorPickerGameScene *layer = ColorPickerGameScene::create(numberOfSeconds);
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}



bool ColorPickerGameScene::init(){
    CountDownGameScene::init();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    buttonArray= CCArray::create();
    buttonArray->retain();
    
    int numberOfButtons = 3;
    double buttonSize = winSize.width * 0.25f;
    double offset = (winSize.width - buttonSize*numberOfButtons)/(numberOfButtons+1);
    for(int i=0;i<numberOfButtons;i++){
        MySpriteButton* iButton = MySpriteButton::create("circle_button.png", (i+1)*offset + i*buttonSize, 0, buttonSize, buttonSize);
        CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(ColorPickerGameScene::onTapButton), CCInteger::create(i));
        setTheRightColorForButton(iButton, i);
        iButton->addCallback(callback);
        this->addChild(iButton);
        buttonArray->addObject(iButton);
    }
    return true;
}

ColorPickerGameScene* ColorPickerGameScene::create(int numberOfSeconds){
    ColorPickerGameScene* scene = ColorPickerGameScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}

void ColorPickerGameScene::setTheRightColorForButton(MySpriteButton *button, int colorIndex){
    switch (colorIndex) {
        case 0:{
            button->setColorState(ccc3(150,0,0), ccc3(255,0,0));
        }
            break;
            
        case 1:{
            button->setColorState(ccc3(0,150,0), ccc3(0,255,0));
        }
            break;
        case 2:{
            button->setColorState(ccc3(0,0,150), ccc3(0,0,255));
        }
            break;
    }
}


void ColorPickerGameScene::onTapButton(cocos2d::CCObject *sender){
    
}

void ColorPickerGameScene::startGame(){
    CountDownGameScene::startGame();
}
void ColorPickerGameScene::reset(){
    CountDownGameScene::reset();
    
    
}

void ColorPickerGameScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    for(int i=0;i<buttonArray->count();i++){
        MySpriteButton* iButton = (MySpriteButton*)buttonArray->objectAtIndex(i);
        iButton->handleTouchesBegan(touches, pEvent);
    }
}

void ColorPickerGameScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    for(int i=0;i<buttonArray->count();i++){
        MySpriteButton* iButton = (MySpriteButton*)buttonArray->objectAtIndex(i);
        iButton->handleTouchesMoved(touches, pEvent);
    }
}

void ColorPickerGameScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    for(int i=0;i<buttonArray->count();i++){
        MySpriteButton* iButton = (MySpriteButton*)buttonArray->objectAtIndex(i);
        iButton->handleTouchesEnded(touches, pEvent);
    }
}
